/**
 * <p>Title: ServiceZMCEU0.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2012-1-16 下午02:43:19  张小飞  Create
 *</pre>
 */
package com.baosight.sczx.zm.ce.service;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.ce.Equipment;
import com.baosight.sczx.zm.ce.domain.ZMCEUnusualProblem;

/**
 * <p>Title: ServiceZMCEU0.java</p>
 * <p>Description: 设备异常问题</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2012-1-16 下午02:43:19  张小飞  Create
 *</pre>
 */
public class ServiceZMCEU0 extends ServiceEPBase {

	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEU0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-16下午02:43:48 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEU0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-16下午02:43:48 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		if(inInfo.getBlock("inqu_status") == null){
			inInfo.addBlock("inqu_status");
		}
		String fdUseDepartmentInqu = inInfo.getBlock("inqu_status").getCellStr(0, "fdUseDepartmentInqu");
		if(fdUseDepartmentInqu == null && "".equals(fdUseDepartmentInqu)){
			StaticTools.setQueryParameters(inInfo, "fdUseDepartment", UserSession.getInSessionProperty("orgName"), true);
		}
		EiInfo outInfo = super.query(inInfo, "ZMCEUnusualProblem.query", new ZMCEUnusualProblem());
		
		outInfo.addBlock(Equipment.getEquipmentBlock("cmes.ce.equipment", null));
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.ce.department", "orgCode", "orgName", false));
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.ce.departmentWithSpace", "orgCode", "orgName", true));		
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.ce.people", "empCode", "empName", false));
		
		outInfo.set("person", UserSession.getInSessionProperty("UserCname"));
		
		return outInfo;
	}

	/**
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEU0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-16下午03:06:35 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_UNUSUALPROBLEM"));
		inInfo.getBlock("result").setCell(0, "fdWorkrecordId", TablePk.getInstance().getNextValue("T_CE_WORKRECORD"));
		inInfo.getBlock("result").setCell(0, "fdProcessPerson", UserSession.getInSessionProperty("UserCname"));
		EiInfo outInfo = super.insert(inInfo, "ZMCEUnusualProblem.insert");
		return outInfo;
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEU0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-16下午03:06:35 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		inInfo.getBlock("result").setCell(0, "fdProcessPerson", UserSession.getInSessionProperty("UserCname"));
		EiInfo outInfo = super.update(inInfo, "ZMCEUnusualProblem.update");
		return outInfo;
	}

	/**
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEU0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-16下午03:30:32 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCEUnusualProblem.delete");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 更新发布状态为"生效"的方法
	 * <p>Function: publish</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-6-2  高强
	 * </p>
	 *
	 */
	public EiInfo publish(EiInfo inInfo) 
	{
		for(int i=0; i < inInfo.getBlock("result").getRowCount(); i++){
			inInfo.getBlock("result").setCell(i, "fdStatus", "未处理");
			inInfo.getBlock("result").setCell(i, "fdSolveDepartment", UserSession.getInSessionProperty("orgName"));
			inInfo.getBlock("result").setCell(i, "fdSolvePerson", UserSession.getInSessionProperty("UserCname"));
		}
		EiInfo _temp = super.update(inInfo, "ZMCEUnusualProblem.updateAfterGenerate");
		EiInfo outInfo = this.query(_temp);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: closeProblem</p>
	 * <p>Description: 关闭问题</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-17上午11:06:43 张小飞
	 * </p>
	 *
	 */
	public EiInfo closeProblem(EiInfo inInfo) {
		for(int i=0; i < inInfo.getBlock("result").getRowCount(); i++){
			inInfo.getBlock("result").setCell(i, "fdStatus", "已处理");
			inInfo.getBlock("result").setCell(i, "fdSolveDepartment", UserSession.getInSessionProperty("orgName"));
			inInfo.getBlock("result").setCell(i, "fdSolvePerson", UserSession.getInSessionProperty("UserCname"));
			inInfo.getBlock("result").setCell(i, "fdProcessDatetime", StaticTools.getDateTime());
		}
		EiInfo _temp = super.update(inInfo, "ZMCEUnusualProblem.updateAfterGenerate");
		//异常问题作废后把对应的故障记录的处理状态改为已处理
		for(int i=0; i < inInfo.getBlock("result").getRowCount(); i++){
			inInfo.getBlock("result").setCell(i, "fdStatus", "已处理");
			inInfo.getBlock("result").setCell(i, "fdKeyId", inInfo.getBlock("result").getCell(i, "fdWorkrecordId"));
		}
		super.update(inInfo, "ZMCEFaultRecord.updateStatus");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
}
